use anyhow::{Context, Result};
use tokio_postgres::{Client, NoTls};
/// Connect to postgres database
pub async fn connect_to_db() -> Result<Client> {
    let postgres_url = std::env::var("POSTGRES_URL").unwrap();
    let postgres_user = std::env::var("POSTGRES_USER").unwrap();
    let postgres_dbname = std::env::var("POSTGRES_DB").unwrap();
    let (client, connection) = tokio_postgres::connect(
        &format!("host={postgres_url} user={postgres_user} dbname={postgres_dbname}"),
        NoTls,
    )
    .await?;

    tokio::spawn(async move {
        connection
            .await
            .context("postgres connection error:")
            .unwrap();
    });

    // let db = client.database("ReadLaterBot");
    Ok(client)
}
